package com.example.demo.alex.service.serviceImpl;

import com.example.demo.alex.model.Person;
import com.example.demo.alex.service.PersonService;
import com.example.demo.alex.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @ClassName PersonDetailsServiceImpl
 * @Description TODO
 * @Author zjg
 * @Date 2018/12/2517:55
 * @Version 2.0
 */
@Component
public class PersonDetailsServiceImpl implements UserDetailsService {
    @Autowired
    PersonService personService;

    @Autowired
    RoleService  roleService;

    @Transactional
    public List<Person> getAllUser(){
        return personService.findAllUser();
    }

    @Transactional
    public Person getByUserName(String userName){
        return personService.findUserByUserName(userName);
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("查找用户");
        Person p=getByUserName(s);
        if(p==null){
            throw  new UsernameNotFoundException("没有该用户");
        }
        return new UserDetailsImpl(p,roleService.findRoleByUserName(p.getUserName()));
    }
}
